package utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.io.IOException;

/**
 * 获取文件内容
 */
public class ReaderFileUtils {
    private static final Logger log = LoggerFactory.getLogger(ReaderFileUtils.class);

    public static String getFileContent(String fileUrl) {
         String fileContent = "";
         FileInputStream fileInputStream = null;
         try {
                 // 1.获取文件指定的文件信息
                 fileInputStream = new FileInputStream(fileUrl);
                 // 2.将数据读到字节数组里
                 byte[] buff = new byte[1024];
                 int length = fileInputStream.read(buff);
                 // 3.将字节数据转换为字符串
                 // 参数一：带转换的字节数组，参数二：起始位置  参数三：转换的长度
                 String info = new String(buff, 0, length);
                 fileContent += info;
                 System.out.println(info);
             } catch (IOException e) {
                log.error("读取文件失败："+e.getMessage());
             } finally {
                 // 4，关闭流操作
                 if (fileInputStream != null){
                    try {
                        fileInputStream.close();
                    }catch (Exception e){
                        log.error("关闭流操作失败",e.getMessage());
                    }
                 }
           }
        return fileContent;
    }
}
